package com.twq.mmail002.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twq.mmail002.entity.Product;
import com.twq.mmail002.entity.ProductCategory;
import com.twq.mmail002.mapper.ProductCategoryMapper;
import com.twq.mmail002.mapper.ProductMapper;
import com.twq.mmail002.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twq.mmail002.service.ProductService;
import com.twq.mmail002.vo.ProductCategoryVO;
import com.twq.mmail002.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author twq
 * @since 2021-08-29
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductCategoryVO> getAllProductCategoryVO() {
//        //实体类转VO  一层处理
//        List<ProductCategory> productCategoryList = productCategoryMapper.selectList(null);
//        //vo
//        ArrayList<ProductCategoryVO> productCategoryVOS = new ArrayList<>();
//        for (ProductCategory productCategory : productCategoryList) {
//            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
////
//            BeanUtils.copyProperties(productCategory,productCategoryVO);
////            productCategoryVO.setId(productCategory.getId());
////            productCategoryVO.setName(productCategory.getName());
//
//
//           productCategoryVOS.add(productCategoryVO);
//        }
//        return productCategoryVOS;
//    }

//一级分类
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type", 1);
        List<ProductCategory> listOne = productCategoryMapper.selectList(queryWrapper);
        List<ProductCategoryVO> listOneVO = listOne.stream()
                .map(e -> new ProductCategoryVO(e.getId(), e.getName()))
                .collect(Collectors.toList());
      //给图片赋值
        //商品信息
        for (int i = 0; i < listOneVO.size(); i++) {
            listOneVO.get(i).setBannerImg("/images/banner"+i+".png");
            listOneVO.get(i).setTopImg("/images/top"+i+".png");

            queryWrapper = new QueryWrapper();
            queryWrapper.eq("categorylevelone_id",listOneVO.get(i).getId());
            List<Product> productList =  productMapper.selectList(queryWrapper);
            List<ProductVO> productVOList = productList.stream()
                    .map(e -> new ProductVO(e.getId(), e.getName(),e.getPrice(),e.getFileName()))
                    .collect(Collectors.toList());
            listOneVO.get(i).setProductVOList(productVOList);
        }

        for (ProductCategoryVO productCategoryVOOne : listOneVO) {
            queryWrapper = new QueryWrapper();
            queryWrapper.eq("type", 2);
            queryWrapper.eq("parent_id", productCategoryVOOne.getId());
            List<ProductCategory> listTwo = productCategoryMapper.selectList(queryWrapper);
            List<ProductCategoryVO> listTwoVO = listTwo.stream()
                    .map(e -> new ProductCategoryVO(e.getId(), e.getName()))
                    .collect(Collectors.toList());
            productCategoryVOOne.setChildren(listTwoVO);

            for (ProductCategoryVO productCategoryVOTwo : listTwoVO) {
                queryWrapper = new QueryWrapper();
                queryWrapper.eq("type", 3);
                queryWrapper.eq("parent_id", productCategoryVOTwo.getId());
                List<ProductCategory> listThree = productCategoryMapper.selectList(queryWrapper);
                List<ProductCategoryVO> listThreeVO = listThree.stream()
                        .map(e -> new ProductCategoryVO(e.getId(), e.getName()))
                        .collect(Collectors.toList());
                productCategoryVOTwo.setChildren(listThreeVO);
            }
        }
        return listOneVO;
    }
}

        //    方式一
//        ArrayList<ProductCategoryVO> listOneVO = new ArrayList<>();
//        for (ProductCategory productCategory : listOne) {
//            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
//            BeanUtils.copyProperties(productCategory,productCategoryVO);
//            listOneVO.add(productCategoryVO);
//        }
       //二级分类
//三级分类




